package utils

import (
	"math/rand"
	"time"
)

const (
	start = 30001
	end   = 32767
)

var r *rand.Rand

func init() {
	seed := rand.NewSource(time.Now().Unix())
	r = rand.New(seed)
}

func GenerateK8sPort(num int) []int {
	var m = make(map[int]bool)
	var ports = make([]int, 0)
	for i := 0; i < num; i++ {
		p := K8sInternalAvailablePort()
		if !m[p] {
			ports = append(ports, p)
		} else {
			i--
		}
		m[p] = true
	}
	return ports
}

//start to end-1
func K8sInternalAvailablePort() int {
	n := end - start
	return start + RandNum(n)
}

func RandNum(n int) int {
	return r.Intn(n)
}
